﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace CompleteReader.Dialogs
{
    public sealed partial class AuthorDialog : UserControl, pdftron.PDF.Tools.Utilities.IAuthorDialog
    {
        public event pdftron.PDF.Tools.Utilities.OnButtonClickedHandler OnButtonClicked;

        public string AuthorName
        {
            get { return AuthorNameTextBox.Text; }
            set { AuthorNameTextBox.Text = value; }
        }

        public AuthorDialog()
        {
            this.InitializeComponent();
            //this.Loaded += AuthorDialog_Loaded;
        }

        void AuthorDialog_Loaded(object sender, RoutedEventArgs e)
        {
            AuthorNameTextBox.Focus(FocusState.Programmatic);
        }

        void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (OnButtonClicked != null)
            {
                OnButtonClicked(this, false);
            }
        }

        void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (OnButtonClicked != null)
            {
                OnButtonClicked(this, true);
            }
        }

        public void Show()
        {
            this.Visibility = Visibility.Visible;
            AuthorNameTextBox.Focus(FocusState.Programmatic);
        }

        public void Hide()
        {
            this.Visibility = Visibility.Collapsed;
        }

        private void AuthorNameTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                if (!string.IsNullOrWhiteSpace(AuthorNameTextBox.Text))
                {
                    if (OnButtonClicked != null)
                    {
                        OnButtonClicked(this, true);
                    }
                }
            }
        }

        private void AuthorNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(AuthorNameTextBox.Text))
            {
                OKButton.IsEnabled = false;
            }
            else
            {
                OKButton.IsEnabled = true;
            }
        }

    }
}
